define( [
  'require',
  'jquery',
  'header',
  'message',
  'constants',
  'h2Api',
  'artCategoryHandle',
  'bigwe_utils',
  'api',
  'tooltip',
  'combiner',
  'nav',
], function( require, $, header, ShowTitleMessage, h2Key, h2Api, category, util, thisAPI ) {
  var myAjax = function( options ) {
    var defaults = {
      type: 'post', //请求的方法,默认为post
    };
    var opt = $.extend( {}, defaults, options );
    $.ajax( {
      type: opt.type,
      url: opt.url || '',
      data: opt.data || '',
      dataType: opt.dataType || 'json',
      before: function() {
        opt.beforefn;
      },
      success: function( data ) {
        var data = ( typeof data === 'string' ) ? $.parseJSON( data ) : data;
        if ( $.isFunction( opt.callBack ) ) {
          opt.callBack( data );
        }
      },
      error: function( XMLHttpRequest, textStatus, errorThrown ) {
        console.log( textStatus );
        console.log( XMLHttpRequest.status );
        console.log( XMLHttpRequest.readyState );
      },
    } )
  };

  var param = {
    url: '/resource/get_recommend',
    data: {
      p: 1,
    },
    callBack: result_list,
    page: true,
    beforefn: ShowTitleMessage.show( '正在加载...' ),
  };

  function result_list( data ) {
    var data = typeof data === 'string' ? $.parseJSON( data ) : data;
    var html = ''; //字符拼接 
    if ( data.ret == 0 ) {
      ( data.data.has_next_page ) ? ( $( ".change" ).hide() ) : ( $( ".change" ).show() );
      var arts = data.data.arts;
      for ( var key in arts ) {
        var art = arts[ key ];

        var url = ( art.cover_img_url != '' ) ? ( h2Key.surpportHostName + '/timthumb.php?w=200&src=' + art.cover_img_url ) : ( h2Key.resPath + '/images/wuyulantu.png' );
        var source_icon = '';

        if ( art.source == 'bigwe.com' ) {
          source_icon = 'icon-iconbigwe'
        } else if ( art.source == 'toutiao.com' ) {
          source_icon = 'icon-icontoutiao'
        } else if ( art.source == 'news.163.com' ) {
          source_icon = 'icon-wangyi'
        } else if ( art.source == 'news.sina.com.cn' ) {
          source_icon = 'icon-xinlang'
        } else if ( art.source == 'ifeng.com' ) {
          source_icon = 'icon-ifeng'
        }

        var cs = art.copyright_stat * 1; //原创标识

        var copyright_img = '';
        if ( cs == 1 || cs == 2 ) {
          if ( art.source == 'toutiao.com' ) {
            copyright_img = '<span class="toutiao_copyright">原创</span>';
          }
          if ( art.source == 'bigwe.com' ) {
            copyright_img = '<img src="' + h2Key.resPath + '/images/original1.png" alt="" class="original"/>';
          }
          if ( art.source == 'mp.weixin.qq.com' ) {
            copyright_img = '<img src="' + h2Key.resPath + '/images/original2.png" alt="" class="original"/>';
          }
        }

        html += '<div class="article-sub">' +
          ' <div class="article-title"><span style="background:' + category.category_color[ category.getBWCode( art.category_code ).code ] + '">' + category.getBWCode( art.category_code ).name + '</span><a href="/article/index/p/' + art.cd + '" target="_blank">' + art.title + '</a></div>';
        if ( art.cover_img_url == '' ) {
          html += '    <div class="picture" style="display:none;">'
        } else {
          html += '    <div class="picture">'
        }
        html += '        <a href="/article/index/p/' + art.cd + '" target="_blank" style="background-image: url(' + url + ')"></a>' +
          '        ' + copyright_img +
          '    </div>';
        if ( art.cover_img_url == '' ) {
          html += '    <div class="article-sub-taxt" data-articleid="' + art.cd + '" data-title="' + art.title + '" data-img="' + url + '" data-url="/article/index/p/' + art.cd + '" data-id="' + art.cd + '" style="width: 100%;">'
        } else {
          html += '    <div class="article-sub-taxt" data-articleid="' + art.cd + '" data-title="' + art.title + '" data-img="' + url + '" data-url="/article/index/p/' + art.cd + '" data-id="' + art.cd + '">'
        }

        html += '        <p>' + ( ( art.summary == "" || art.summary == null ) ? art.summary = "" : art.summary ) + '</p>';
        if ( art.source == 'mp.weixin.qq.com' ) {
          html += '<div class="btn_plat" onclick="window.open(\'/profile?biz=' + util.getUrlParam( art.source_url, '__biz' ) + '&gn=' + escape( art.publisher ) + '&platform=' + art.source + '\')"><span class="iconfont icon-weixin"></span>' +
            '<span class="article-account">' + art.publisher + '</span></div>';
        } else {
          html += '<span class="iconfont platform-icon ' + source_icon + '"></span>' +
            '<span class="article-account">' + art.publisher + '</span>';
        }

        html += '<span class="article-time">' + ( new Date( art.publish_time_millis * 1 ) ).format( 'yyyy-MM-dd hh:mm' ) + '</span>' +
          '        <span class="iconfont icon-collect article-collect" data-toggle="tooltip" title="收藏"></span>' +
          '        <span class="iconfont icon-box article-merge" addcar-event data-toggle="tooltip" title="加入合成器"></span>' +
          '    </div>' +
          '</div>';
      }
    }

    $( '.reco_list' ).append( html );
    $( '[data-toggle="tooltip"]' ).tooltip( {
      trigger: 'hover'
    } );
    ShowTitleMessage.hide();
  }
  myAjax( param );
  $( '.change' ).click( function() {
    param.data.p += 1;
    myAjax( param );
    $( 'html,body' ).animate( {
      scrollTop: '0px'
    }, 400 );
    $( this ).hide();
    $( ".reco_list" ).empty();
  } );

  /*文章收藏点击*/
  $( ".reco_list" ).on( "click", ".article-collect", function() {
    $( this ).addClass( "golden" );
    var re = $( this );
    var articleID = $( this ).parent().attr( 'data-articleID' );
    var collect_param = {
      url: '/favorites/add',
      data: {
        cd: articleID,
        rt: "collectArt",
      },
      callBack: collect,
    };
    myAjax( collect_param );
  } );

  function collect( data ) {
    var data = typeof data === 'string' ? $.parseJSON( data ) : data;
    if ( data.ret == 0 ) {
      ShowTitleMessage.show( '收藏成功!', 1000 );
    } else {
      ShowTitleMessage.show( data.msg, 1000 );
      re.removeClass( "golden" );
    }
  };
} );